package translinkApp.Adaptors.Contacts;

import java.io.InputStream;
import java.util.ArrayList;

import translinkApp.Activities.R;
import translinkApp.DataTypes.Contacts.ContactAddress;
import translinkApp.DataTypes.Contacts.ContactAddressList;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ContactAddressAdaptor extends BaseAdapter {

	private Context context;
	private ArrayList<ContactAddressList> contactAddresses;

	public ContactAddressAdaptor(final Context context,
			ArrayList<ContactAddressList> contactAddresses) {

		this.contactAddresses = contactAddresses;
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ContactAddressList c = (ContactAddressList) getItem(position);

		// if the array item is null, nothing to display, just return null
		if (c == null) {
			return null;
		}
		Activity a = ((Activity) context);
		View v = a.getLayoutInflater().inflate(R.layout.contact_row, null);

		ImageView picture = (ImageView) v.findViewById(R.id.contact_row_image);

		Bitmap contactImage = loadContactPhoto(context.getContentResolver(), c.getContactID());

		if (contactImage == null) {
			contactImage = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.nocontactpicture);
		}

		picture.setImageBitmap(contactImage);

		TextView nameText = (TextView) v.findViewById(R.id.contact_row_name);
		nameText.setText(c.getName());

		TextView addressText = (TextView) v.findViewById(R.id.contact_row_address);
		addressText.setText(c.get(0).getFullAddress());

		TextView moreAddressText = (TextView) v.findViewById(R.id.contact_row_more_address);

		if (c.size() > 1) {
			moreAddressText.setText((c.size() - 1) + " more addresses");
		} else {
			moreAddressText.setVisibility(View.GONE);
		}

		return v;
	}

	public static Bitmap loadContactPhoto(ContentResolver cr, long id) {

		Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);

		InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);

		if (input == null) {

			return null;

		}

		return BitmapFactory.decodeStream(input);

	}

	@Override
	public int getCount() {

		return contactAddresses.size();
	}

	@Override
	public Object getItem(int position) {

		return contactAddresses.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

}